package com.hx.musicserver.controller;

import com.hx.common.vo.Result;
import com.hx.musicserver.entity.Music;
import com.hx.musicserver.service.MusicService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mu/api")
public class MusicController {
    @Autowired
    private MusicService musicService;

    @ApiOperation(value = "添加音乐")
    @PostMapping("/save")
    public Result save(Music music){
        music.setCreateTime(new Date());
        boolean save= musicService.save(music);
        return save?Result.ok():Result.fail("添加失败");
    }

    @ApiOperation(value = "查询所有")
    @Cacheable(value = "MusicCache",key = "'music.getAllMusic'")
    @GetMapping("/getall")
    public Result getAll(){
        System.out.println("正在调用数据库进行查询");
        List<Music> list = musicService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "更改音乐信息")
    @PostMapping("/update")
    public Result update(Music music){
        boolean updateById = musicService.updateById(music);
        return updateById?Result.ok():Result.fail("修改失败");
    }

    @ApiOperation(value = "删除音乐")
    @CacheEvict(value = "MusicCache", key = "'music.getAllMusic'")
    @PostMapping("/delete")
    public Result delete(Music music){
        boolean removeById=musicService.removeById(music);
        return removeById?Result.ok():Result.fail("删除失败");
    }

    @ApiOperation(value = "查询音乐收藏")
    @GetMapping("likeMusic")
    public Result findMusicByUser(Integer musicId){
        return Result.ok(musicService.findMusicById(musicId));
    }

}
